﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Thêm thư viện
using KaraokeManagementDTO;
using System.Data.OleDb;
using System.Data;

namespace KaraokeManagementDAO
{
    public class PhieuTiepNhanDatPhongDAO:DataProvider
    {
        public Boolean themPhieu(PhieuTiepNhanDatPhongDTO dto)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van them phieu
            String query = "insert into PhieuTiepNhanDatPhong(KhachHang_ID,ThoiLuongSuDung,NhanVien_ID,ThoiGianVao,ThoiGianRa,TinhTrang,PhongHat_ID) values(@KhachHang_ID,@ThoiLuongSuDung,@NhanVien_ID,@ThoiGianVao,@ThoiGianRa,@TinhTrang,@PhongHat_ID)";

            para.Add(new OleDbParameter("@KhachHang_ID", dto.KhachHang_ID));
            para.Add(new OleDbParameter("@ThoiLuongSuDung", dto.ThoiLuongSuDung));
            para.Add(new OleDbParameter("@NhanVien_ID", dto.NhanVien_ID));
            para.Add(new OleDbParameter("@ThoiGianVao", dto.ThoiGianVao));
            para.Add(new OleDbParameter("@ThoiGianRa", dto.ThoiGianRa));
            para.Add(new OleDbParameter("@TinhTrang", dto.TinhTrang));
            para.Add(new OleDbParameter("@PhongHat_ID", dto.PhongHat_ID));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;

        }

        public Boolean xoaPhieu(int MaPhieu)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van xoa phieu
            String query = "delete from PhieuTiepNhanDatPhong where ID=@MaPhieu";

            para.Add(new OleDbParameter("@ID", MaPhieu));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public Boolean capNhatPhieu(PhieuTiepNhanDatPhongDTO dto)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van them phieu
            String query = "update PhieuTiepNhanDatPhong set KhachHang_ID=@KhachHang_ID,ThoiLuongSuDung=@ThoiLuongSuDung,NhanVien_ID=@NhanVien_ID,ThoiGianVao=@ThoiGianVao,ThoiGianRa=@ThoiGianRa,TinhTrang=@TinhTrang,PhongHat_ID=@PhongHat_ID where ID=@ID";

            para.Add(new OleDbParameter("@KhachHang_ID", dto.KhachHang_ID));
            para.Add(new OleDbParameter("@ThoiLuongSuDung", dto.ThoiLuongSuDung));
            para.Add(new OleDbParameter("@NhanVien_ID", dto.NhanVien_ID));
            para.Add(new OleDbParameter("@ThoiGianVao", dto.ThoiGianVao));
            para.Add(new OleDbParameter("@ThoiGianRa", dto.ThoiGianRa));
            para.Add(new OleDbParameter("@TinhTrang", dto.TinhTrang));
            para.Add(new OleDbParameter("@PhongHat_ID", dto.PhongHat_ID));
            para.Add(new OleDbParameter("@ID", dto.ID));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public PhieuTiepNhanDatPhongDTO layThongTinPhieu(int MaPhieu)
        {
            PhieuTiepNhanDatPhongDTO phieu = new PhieuTiepNhanDatPhongDTO();
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van lay thong tin chi tiet
            String query = "select * from PhieuTiepNhanDatPhong where ID=@MaPhieu";

            para.Add(new OleDbParameter("@ID", MaPhieu));

            //DataTable
            DataTable dt = new DataTable();
            dt = ExecuteQuery(query, para);

            if (dt.Rows.Count == 0)
            {
                return null;
            }

            DataRow row = dt.Rows[0];

            phieu.ID = int.Parse(row["ID"].ToString());
            phieu.KhachHang_ID = int.Parse(row["KhachHang_ID"].ToString());
            phieu.ThoiLuongSuDung = int.Parse(row["ThoiLuongSuDung"].ToString());
            phieu.NhanVien_ID = int.Parse(row["NhanVien_ID"].ToString());
            phieu.ThoiGianVao = DateTime.Parse(row["ThoiGianVao"].ToString());
            phieu.ThoiGianRa = DateTime.Parse(row["ThoiGianRa"].ToString());
            phieu.TinhTrang = int.Parse(row["TinhTrang"].ToString());
            phieu.PhongHat_ID = int.Parse(row["PhongHat_ID"].ToString());

            return phieu;
        }

        public List<PhieuTiepNhanDatPhongDTO> layDanhSachPhieu()
        {
            List<PhieuTiepNhanDatPhongDTO> listPhieu = new List<PhieuTiepNhanDatPhongDTO>();

            List<OleDbParameter> para = new List<OleDbParameter>();

            //Câu truy vấn lấy dữ liệu
            String query = "select * from PhieuTiepNhanDatPhong";


            //DataTable
            DataTable dt = new DataTable();
            dt = ExecuteQuery(query, para);

            if (dt.Rows.Count == 0)
            {
                return null;
            }

            foreach (DataRow row in dt.Rows)
            {
                PhieuTiepNhanDatPhongDTO dto = new PhieuTiepNhanDatPhongDTO();

                dto.ID = int.Parse(row["ID"].ToString());
                dto.KhachHang_ID = int.Parse(row["KhachHang_ID"].ToString());
                dto.ThoiLuongSuDung = int.Parse(row["ThoiLuongSuDung"].ToString());
                dto.NhanVien_ID = int.Parse(row["NhanVien_ID"].ToString());
                dto.ThoiGianVao = DateTime.Parse(row["ThoiGianVao"].ToString());
                dto.ThoiGianRa = DateTime.Parse(row["ThoiGianRa"].ToString());
                dto.TinhTrang = int.Parse(row["TinhTrang"].ToString());
                dto.PhongHat_ID = int.Parse(row["PhongHat_ID"].ToString());
                listPhieu.Add(dto);
            }

            return listPhieu;
        }

        public int layPhieuDatPhongMoiNhat()
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Câu truy vấn lấy dữ liệu
            String query = "select max(ID) from PhieuTiepNhanDatPhong";


            //DataTable
            object obj = new object();
            obj = ExecuteScalar(query, para);

            if (obj == null)
                return -1;

            return int.Parse(obj.ToString());
        }


        public int layPhieuDatPhongHienTai(int MaPhong)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Câu truy vấn lấy dữ liệu
            String query = "select max(ID) from PhieuTiepNhanDatPhong where PhongHat_ID=@MaPhong";

            para.Add(new OleDbParameter("@MaPhong",MaPhong));

            //DataTable
            object obj = new object();
            obj = ExecuteScalar(query, para);

            if (obj == null)
                return -1;

            return int.Parse(obj.ToString());
        }
    }
}
